const skinBehavior = require("../../../utils/themeSkin/skinBehavior")
var app = getApp();
Page({
  behaviors: [skinBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    Height: 0,
    LastHeight: 0,
    choose_index: 1,
    SearchValue: "",

    current_page: 1,
    last_page: 1,
    list: [],

    CarouseList: [],

    language: "",
    order_type: 0,
    search_type: 0,

    sort: {
      up: 0,
      down: 0
    },

    category_list: [],
    category_10: [],
    category_index: 0,
    cate_id:'',//分类id
    show_dy_password:false,
    dy_password:"",

    show_flag:true,
    album_id:"",

  },
  close_show_dy(){
    this.setData({show_dy_password:false})
  },
  code_link() {
    wx.setClipboardData({
      data: this.data.dy_password,
      success: () => {
        app.tips('复制成功')
        this.setData({show_dy_password:false})
      },
      fail: (err) => {
        app.tips('复制失败')
      }
    });
  },
  open_dialog(e){
    console.log(e,"e==");

    app._getNetWork({
      url:app.getNetAddresss("plugin.tiktok-cps.api.goods.aggregate-h5"),
      data:{material_id:e.detail.material_id},
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result){
          this.setData({
            dy_password : data.command,
            activity_name:e.detail.name,
            show_dy_password:true
          })
        }else{
          app.tips(msg);
        }
      }
    })
  },
  getCategoryGoods(e) {
    let cate_id = e.currentTarget.dataset.id;
    let category_index =  e.currentTarget.dataset.index;
    this.setData({category_index,cate_id})
    this.getData({
      page: 1,
      order_type: this.data.order_type,
      search_type: this.data.search_type,
      cate_id
    })
  },

  imgHeight: function (e) {
    var winWid = wx.getSystemInfoSync().windowWidth; //获取当前屏幕的宽度
    if (e && e.detail) {
      var imgh = e.detail.height; //图片高度
      var imgw = e.detail.width; //图片宽度
      var swiperH = (winWid * imgh) / imgw; //等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
      if (parseInt(swiperH) > this.data.LastHeight) {
        this.setData({
          Height: swiperH, //设置高度
        });
        this.data.LastHeight = parseInt(swiperH);
      }
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.album_id){
      this.setData({show_flag:false,album_id:options.album_id})
    }
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en,
    });
    this.getCarouse();
    this.getCategory();
    this.buttonGroup()
    this.getData({
      page: 1
    });
    this.imgHeight()
  },
  getCategory() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.tiktok-cps.api.goods.good-recommend-cate'),
      success: resdata => {
        if (resdata.data.result == 0) {
          return app.tips(result.data.msg)
        }
        this.setData({
          category_list: resdata.data.data || []
        })
      }
    })
  },
  buttonGroup() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.tiktok-cps.api.goods.button-group'),
      success: resdata => {
        if (resdata.data.result == 0) {
          return app.tips(result.data.msg)
        }
        if( resdata.data.data &&  resdata.data.data.length>=1){
          let category_10 = this.chunk( resdata.data.data, 10);
          this.setData({
            category_10
          })
        }

      }
    })
  },
  chunk(arr, n) {
    var result = [];
    for (var i = 0, len = arr.length; i < len; i += n) {
      result.push(arr.slice(i, i + n));
    }
    return result;
  },
  link_detail(e) {
    let goods_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageF/cps/detail/detail?goods_id=' + goods_id,
    })
  },
  getCarouse() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.tiktok-cps.api.rotation.rotation-list'),
      success: resdata => {
        if (resdata.data.result == 0) {
          return app.tips(resdata.data.msg)
        }
        this.setData({
          CarouseList: resdata.data.data || []
        })
      }
    })
  },
  choose(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      choose_index: index
    })

    if (index !== 1) {
      if (this.data.sort.up == 0 && this.data.sort.down == 0) {
        this.setData({
          "sort.up": 1,
          "sort.down": 0
        })
      } else if (this.data.sort.up == 1 && this.data.sort.down == 0) {
        this.setData({
          "sort.up": 0,
          "sort.down": 1
        })
      } else if (this.data.sort.up == 0 && this.data.sort.down == 1) {
        this.setData({
          "sort.up": 1,
          "sort.down": 0
        })
      }
    } else if (index == 1) {
      this.setData({
        "sort.up": 0,
        "sort.down": 0
      })
    }
    if (this.data.sort.down == 1) {
      this.setData({
        order_type: 1
      })
    } else {
      this.setData({
        order_type: 0
      })

    }
    let search_type = index - 1;
    this.setData({
      search_type
    })
    this.getData({
      page: 1,
      order_type: this.data.order_type,
      search_type,
      cate_id:this.data.cate_id
    })
  },
  link(e) {
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url,
    })
  },
  getData(json) {
    let url = "plugin.tiktok-cps.api.goods.good-list";
    if(!this.data.show_flag){
      let {page }= json;
      json = {
        page,
        album_id:this.data.album_id,
      }
      url = "plugin.tiktok-cps.api.goods.album-good-list";
    }
    app._getNetWork({
      url: app.getNetAddresss(url),
      data: json,
      success: resdata => {
        let data = resdata.data;
        if (data.result == 0) {
          return app.tips(data.msg)
        }
        if (json.page == 1) {
          this.setData({
            list: data.data.products
          })
        } else if (json.page > 1) {
          let list = this.data.list.concat(data.data.products)
          this.setData({
            list
          })
        }
        this.setData({
          current_page: data.data.current_page,
          last_page: data.data.last_page,
        })
      }
    })
  },
  getMoreData() {
    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let page = Number(this.data.current_page) + 1;
    this.getData({
      page,
      order_type: this.data.order_type,
      search_type: this.data.search_type,
      cate_id:this.data.cate_id
    })
  },
  link_search() {
    wx.navigateTo({
      url: '/packageF/cps/search/search',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMoreData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})